<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<div id="app">
    hello
</div>
<script>
  let data = {
    msg: 'hello',
    count: 10,
  }
  // 模拟vue实例
  let vm = {}
  //获取多个对象的key，并且做数据劫持
  Object.keys(data).forEach(key => {
    Object.defineProperty(vm, key, {
      enumerable: true,
      configurable: true,
      get() {
        console.log('get:', data[key])
        return data[key]
      },
      set(newValue) {
        console.log('set: ', key, newValue)
        if (data[key] === newValue) {
          return
        }
        data[key] = newValue
        document.querySelector('#app').textContent = data[key]
      }
    })
  })
  // 测试
  vm.msg = 'Hello World'
  console.log(vm.msg)
</script>
</body>
</html>
